﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using InspectionMAC.SystemThread;
using InspectionMAC.Function;

namespace InspectionMAC.SystemThread
{
    class AddOverflowThread
    {
        public static bool bAddOverflow = false;
        public static bool bAddOverflow02 = false;

        public static Thread AddOverflow_Thread = null;
        public static Thread AddOverflow_Thread02 = null;

        public static List<uint> LPos = new List<uint>();       //--這個記錄比較位置
        public static List<uint> Ldir = new List<uint>();       //--這個記錄比較模式(0為小於等於  /  1為大於等於)，可以直接預設1
        public static List<ushort> LAction = new List<ushort>();  //--這個記錄DO執行模式(正式使用5為一次500us脈波或6為一次1ms脈波)
        public static List<ushort> LActpara = new List<ushort>(); //--這個記錄DO號
        public static List<uint> Laxis = new List<uint>();     //---這個用來記錄那個軸

        public static List<OverFlowInfo> overFlowInfos = new List<OverFlowInfo>();
        public static List<OverFlowInfo> overFlowInfos02 = new List<OverFlowInfo>();
        public static void AddOverflow_State()
        {
            bAddOverflow = true;
            AddOverflow_Thread = new Thread(AddOverflow_Run);
            AddOverflow_Thread.IsBackground = true;
            AddOverflow_Thread.Start();

            bAddOverflow02 = true;
            AddOverflow_Thread02 = new Thread(AddOverflow_Run02);
            AddOverflow_Thread02.IsBackground = true;
            AddOverflow_Thread02.Start();
        }
        private static void AddOverflow_Run(object obj)
        {
            while (bAddOverflow)
            {
                if (LocationThread.EncoderCont[0] < 1000)
                {
                    MotionStatus_API.bOverflow = false;
                    ///----在溢位後，將目前未添加的比較位置都一次添加進去----------------------------
                    for (int i = 0; i <= LPos.Count; i++)
                    {
                        UInt32 UPos = LPos[i];
                        ushort Action = LAction[i];
                        ushort Actpara = LActpara[i];
                        MotionStatus_API.AddOverflowLocation(0, 0, UPos, 1, Action, Actpara);
                    }
                    for (int i = 0; i < overFlowInfos.Count; i++)
                    {
                        MotionStatus_API.AddOverflowLocation(0, 0, overFlowInfos[i].LPos, 1, overFlowInfos[i].LAction, overFlowInfos[i].LActpara);
                    }
                    ///---添加後，都把list給清除了，等待下一次溢位時進行記錄-------------------------
                    LPos.Clear();
                    Ldir.Clear();
                    LAction.Clear();
                    LActpara.Clear();
                    Laxis.Clear();

                    overFlowInfos.Clear();
                    ///-------------------------------------------------------------------------------
                    bAddOverflow = false;
                }
                Thread.Sleep(1);
            }
        }

        private static void AddOverflow_Run02(object obj)
        {
            while (bAddOverflow02)
            {
                if (LocationThread.EncoderCont[1] < 1000)
                {
                    MotionStatus_API.bOverflow02 = false;
                    ///----在溢位後，將目前未添加的比較位置都一次添加進去----------------------------

                    for (int i = 0; i < overFlowInfos02.Count; i++)
                    {
                        MotionStatus_API.AddOverflowLocation(0, 1, overFlowInfos02[i].LPos, 1, overFlowInfos02[i].LAction, overFlowInfos02[i].LActpara);
                    }
                    ///---添加後，都把list給清除了，等待下一次溢位時進行記錄-------------------------

                    overFlowInfos02.Clear();
                    ///-------------------------------------------------------------------------------
                    bAddOverflow02 = false;
                }
                Thread.Sleep(1);
            }
        }
    }
    class OverFlowInfo
    {
        public uint LPos;
        public uint Ldir;
        public ushort LAction;
        public ushort LActpara;
        public uint Laxis;
    }
}
